


//
//  MovieCollectionViewController.m
//  DSYDouban
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MovieCollectionViewController.h"
#import "MovieCollectionCell.h"

#import "UIImageView+WebCache.h"
#import "MovieCollectionViewCell.h"

@interface MovieCollectionViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArr;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) NSInteger rowNum;

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation MovieCollectionViewController

- (void)dealloc
{
    [_moviewModel release];
    [_movieArr release];
    [_tableView release];
    [_collectionView release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"电影";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:nil] autorelease];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    
    
    
    
    // 创建collectionView
    [self createCollectionView];
    
    // 创建tabview
//    [self createTableView];
    
    [self dataHandle];
    
}




// 数据加载
- (void)dataHandle {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
    
    self.movieArr = [NSMutableArray array];
    
    for (NSDictionary *dic in tempArr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_movieArr addObject:model];
    }
    
}

- (void)rightButtonItemAction: (UIBarButtonItem *)buttonItem {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createCollectionView {
    
    //
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 每一个cell(item)大小
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 40) / 3, (self.view.bounds.size.width - 40) / 3 * 1.5);
    // 最小行间距
    layout.minimumLineSpacing = 10;
    // 最小列间距
    layout.minimumInteritemSpacing = 9.0;
    // 与其四周的间距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //
    
    // 创建对象
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [layout release];
    [_collectionView release];
    
}

#pragma mark - UICollectionViewDelegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor greenColor];
    MovieModel *model = _movieArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _movieArr.count;
}


#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // 如果行数下标小于 总行数-1, 或则总行数是3的倍数，每一行都有三个图
    if (indexPath.row < _rowNum - 1 || _movieArr.count % 3 == 0) {
        MovieModel *model1 = _movieArr[indexPath.row * 3];
        MovieModel *model2 = _movieArr[indexPath.row * 3 + 1];
        MovieModel *model3 = _movieArr[indexPath.row * 3 + 2];
        
        [cell.movieImageView1 sd_setImageWithURL:[NSURL URLWithString:[model1.images objectForKey:@"large"]]];
        cell.movieNameLabel1.text = model1.title;
        
        [cell.movieImageView2 sd_setImageWithURL:[NSURL URLWithString:[model2.images objectForKey:@"large"]]];
        cell.movieNameLabel2.text = model2.title;
        
        [cell.movieImageView3 sd_setImageWithURL:[NSURL URLWithString:[model3.images objectForKey:@"large"]]];
        cell.movieNameLabel3.text = model3.title;
    }
    
    //
    if (indexPath.row == _rowNum - 1 && _movieArr.count % 3 != 0) {
        
        if (_movieArr.count - 3 * (_rowNum - 1) == 1) {
            MovieModel *model1 = _movieArr[indexPath.row * 3];
            
            [cell.movieImageView1 sd_setImageWithURL:[NSURL URLWithString:[model1.images objectForKey:@"large"]]];
            cell.movieNameLabel1.text = model1.title;
            
            // 第二张和第三张图不显示
            cell.movieImageView2.image = nil;
            cell.movieNameLabel2.text = @"";
            
            cell.movieNameLabel3.text = @"";
            cell.movieImageView3.image = nil;
            
        }
        if (_movieArr.count - 3 * (_rowNum - 1) == 2) {
            
            MovieModel *model1 = _movieArr[indexPath.row * 3];
            MovieModel *model2 = _movieArr[indexPath.row * 3 + 1];
            
            [cell.movieImageView1 sd_setImageWithURL:[NSURL URLWithString:[model1.images objectForKey:@"large"]]];
            cell.movieNameLabel1.text = model1.title;
            
            [cell.movieImageView2 sd_setImageWithURL:[NSURL URLWithString:[model2.images objectForKey:@"large"]]];
            cell.movieNameLabel2.text = model2.title;
            
            cell.movieNameLabel3.text = @"";
            cell.movieImageView3.image = nil;
        }
    }
    
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (_movieArr.count % 3 == 0) {
        _rowNum = (NSInteger)(_movieArr.count / 3);
        return _rowNum;
        
    } else {
        
        _rowNum = (NSInteger)(_movieArr.count / 3) + 1;
        return _rowNum;
    }
}


- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView release];
    
    [_tableView registerClass:[MovieCollectionCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
